package com.base.system.controller.sys;

import com.base.system.base.http.HttpResult;
import com.base.system.jpa.entity.JpaUser;
import com.base.system.service.MenuService;
import com.base.system.web.dto.menu.MenuDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单区")
@RestController
@RequestMapping("/sys/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过自身id获取菜单集合")
    HttpResult<List<MenuDto>> findById(@PathVariable Long id) {
        return HttpResult.ok(menuService.findAllById(id));
    }

    @GetMapping("/_all")
    @ApiOperation(value = "所有菜单集合")
    @PreAuthorize("hasAuthority('menu_list')")
    HttpResult<List<MenuDto>> menuAll() {
        return HttpResult.ok(menuService.all());
    }

    @GetMapping("/currentMenu")
    @ApiModelProperty(value = "获取当前用户的菜单")
    HttpResult<List<MenuDto>> currentMenu() {
        return HttpResult.ok(menuService.currentMenu());
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAuthority('menu_update_save')")
    HttpResult<JpaUser> save(@RequestBody MenuDto menu) {
        menuService.save(menu);
        return HttpResult.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新")
    @PreAuthorize("hasAuthority('menu_update_save')")
    HttpResult<JpaUser> update(@RequestBody MenuDto menu) {
        menuService.update(menu);
        return HttpResult.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAuthority('menu_delete')")
    HttpResult<Void> delete(@PathVariable Long id) {
        menuService.delete(id);
        return HttpResult.ok();
    }
}
